<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;


class goodsDetailController extends CommonController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {   
        //判断是否搜索了
        if(isset($_GET['id'])){
            
            $id = $_GET['id'];
            $list = \DB::table('goodsdetail')->where('id',$id)->paginate(2);
        }else{

        //获取然后进行2个分页
        $list = \DB::table('goodsdetail')->paginate(2);
        }

        
        //我现在要知道我有哪些详情是空的并且来自那张表
        $nodetail = \DB::table('goodsdetail')->where('gooddetail',null)->get();

        //记录有多少条数据我要补齐的
        $linyy = 0;
        $liuqi = 0;

        //遍历，因为可能有多个嘛！
        foreach($nodetail as $v){
            if($v->status==2){
                // $v->whois = "林莹莹";
                // $v->fromtable = "newbook";
                $linyy++;

                //获取那条id
                $linid[] = $v->id;
            }
            if($v->status==4){
                // $v->whois = "刘奇";
                // $v->fromtable = "rank";
                $liuqi++;

                //获取那条id
                $liuid[] = $v->id;
            }
        }

        //处理数据
        $linid = implode(',',$linid);
        $liuid = implode(',',$liuid);
        $ondetail['lin'] = $linyy;
        $ondetail['liu'] = $liuqi;
        $ondetail['linid'] = $linid;
        $ondetail['liuid'] = $liuid;

        return view('admin.goodsDetail.index',['list'=>$list,'task'=>$ondetail]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin.goodsDetail.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $_POST;
        unset($data['_token']);
        $m = \DB::table('goodsdetail')->insertGetId($data);
        if($m>0){
            return view('admin.goodsDetail.info',['info'=>"插入成功"]);
        }else{
            return view('admin.goodsDetail.info',['info'=>"插入失败"]);
        }
        
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $data = $_GET;
        $id = $_GET['id'];
        $name = $_GET['name'];
        $value = $_GET['value'];
        \DB::table('goodsdetail')->where('id',$id)->update([$name=>$value]);
        return "done";
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $list = \DB::table('goodsdetail')->where('id',$id)->first();
        return view('admin.goodsDetail.edit',['list'=>$list]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        // dd($_POST);
        $data = $_POST;
        unset($data['_method']);
        unset($data['_token']);
        $m = \DB::table('goodsdetail')->where('id',$id)->update($data);
        if($m>0){
            return view('admin.goodsDetail.info',['info'=>'嗯，你修改成功了呢']);
        }else{
            return view('admin.goodsDetail.info',['info'=>'失败了失败了失败了~~']);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        // dd($id);
        $m = \DB::table('goodsdetail')->where('id',$id)->delete();
        if($m){
            return view('admin.goodsDetail.info',['info'=>'删掉了！！']);
        }else{
            return view('admin.goodsDetail.info',['info'=>'没有删掉！！']);
        }
    }

    public function doUp(Request $request)
    {   

        // dd($_FILES);
        $id = $_GET['id'];
        $bj = $_GET['bj'];
        $bj = 'pic'.$bj;
       if ($request->hasFile('myfile')) {
            $filename = time().rand(1000,9999).'.jpg';
            $request->file('myfile')->move('./uploads/admin/',$filename);
            
            \DB::table('goodsdetail')->where('id',$id)->update([$bj=>$filename]);
            return $filename;
       }

       return "1.jpg";
    }

    public function xx()
    {
        $list = \DB::table('goodsdetail')->where('id',$_GET['id'])->first();
        return view('admin.goodsDetail.detail',['list'=>$list]);
    }
}
